;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Sales.DealerLimit", [])
        .controller("DealerLimitCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "Upload", "$timeout", "EventBus", "$filter", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService, Upload, $timeout, EventBus, $filter) {
            $scope.markInfo = {
                allChecked: false,
                curRow: 0
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            $scope.dataList = [];
            $scope.condition = {};
            $scope.btnEnabled = true;
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                showMateriel: false
            };
            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/dealerLimit/exportAll")
            };
            $scope.tableConfig = {
                fixedWidth: 120,
                scrollWidth: 1000,
                tableCol: [
                    {
                        type: "",
                        name: "客户编号",
                        dataField: "dealerCode",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户名称",
                        dataField: "companyName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户代码",
                        dataField: "dealerOrgId",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户分类",
                        dataField: "dealerClassName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户简称",
                        dataField: "shortName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        name: "可用额度",
                        dataField: "enableMoney",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: 'mainLimitLev',
                        name: "额度控制级别",
                        dataField: "mainLimitLev",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "已用额度",
                        dataField: "usedLimit",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "totalLimit",
                        name: "总额度",
                        dataField: "totalLimit",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 400,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            var btnPermissionCode = {
                dealerLimitSave: window.ActionCode.dealerLimitSave,
                dealerLimitImport: window.ActionCode.dealerLimitImport,
                dealerLimitExport: window.ActionCode.dealerLimitExport
            };

            $scope.btnPermission = {
                dealerLimitSave: true,
                dealerLimitImport: true,
                dealerLimitExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {left: $scope.tableConfig.fixedWidth + "px"}
                };
            };

            $scope.selectAll = function (list) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = $scope.markInfo.allChecked;
                }
            };

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.setEnableBtn = function (code, isEnabled) {
                var codeList = [code];
                setEnableFunc(codeList, isEnabled);
            };

            $scope.setEnableBatchBtn = function (isEnabled) {
                var codeList = [];
                angular.forEach($scope.dataList, function (item) {
                    if (item.isChecked) {
                        codeList.push(item.id);
                    }
                });
                setEnableFunc(codeList, isEnabled);
            };

            function setEnableFunc(CodeList, isEnabled) {
                var msg = isEnabled === 0 ? "禁用" : "启用";
                if (CodeList.length == 0) {
                    ybWidget.ShowMessage('请选择要' + msg + '的数据');
                    return;
                }

                ybWidget.ShowMessage("确定要" + msg + "吗？", MESG_TYPE.Ask).then(function (data) {
                    var service = "device";
                    ybHttp.Post(service, '/dealerLimit/updateEnableLimit', {
                        idArray: CodeList,
                        enableLimit: isEnabled
                    }).then(function (data) {
                        $scope.markInfo.allChecked = false;
                        if (data.code !== 0) {
                            ybWidget.ShowMessage(data.message);
                            return;
                        }
                        getDataList();
                    })
                })
            }


            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/dealerLimit/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = $scope.condition;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    angular.forEach(resp.value.list, function (item) {
                        item.oldLimit = item.totalLimit;
                        item.isChecked = false;
                    });
                    $scope.markInfo.allChecked = false;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.importDate = function () {
                var param = {
                    name: "客户额度",
                    templateReqService: "device",
                    templateReqUrl: "/dealerLimit/downloadLimitExcel",
                    importReqService: "device",
                    importReqUrl: "/dealerLimit/importLimitExcel",
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getDataList(1);
                });
            };

            $scope.onColDataChange = function () {
                $scope.btnEnabled = false;
            };

            $scope.saveData = function () {
                var service = "device";
                var reqUrl = "/dealerLimit/updateLimit";
                ybHttp.Post(service, reqUrl, $scope.dataList).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                });
            };


            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);

                if (!$scope.isJCH) {
                    $scope.tableConfig.tableCol = ybWidget.HideTableCols($scope.tableConfig.tableCol, ['enableMoney', 'mainLimitLev']);
                }
            })();
        }])
    ;
})(window, window.angular);